package com.yunbiao.ybbusstopplate.play_sync;

import com.alibaba.fastjson.JSONObject;
import com.xuhao.didi.core.iocore.interfaces.ISendable;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;

public class BaseSendData implements ISendable {

    private final String data;

    public BaseSendData(JSONObject jsonObject) {
        this(jsonObject.toJSONString());
    }

    public BaseSendData(String data) {
        this.data = data;
    }

    @Override
    public byte[] parse() {
        //根据服务器的解析规则,构建byte数组
        byte[] body = data.getBytes(Charset.defaultCharset());
        ByteBuffer bb = ByteBuffer.allocate(4 + body.length);
        bb.order(ByteOrder.BIG_ENDIAN);
        bb.putInt(body.length);
        bb.put(body);
        return bb.array();
    }
}
